import { createStore } from 'vuex'

export default createStore({
  state: {
    //标签
    tags:[],
    //上一个活跃组件
    lastActiveComponent:"",
    //前端页面配置
    user:{},

    // --------------------
    //下面为管理员后台配置
    // 缓存组件页面
    catch_components: [],
    // 全局请求后台URL
    baseURL: 'http://localhost:8080',
    // 当前选中的菜单 - 默认选择首页
    activePath: 'adminhome',
    // 菜单项 - 默认包含首页
    tabList: [
        {path: 'adminhome', label: '系统首页', name: 'adminhome'}
    ]
  },
  getters: {
  },
  mutations: {
    setTags(state,tags){
        state.tags = tags
    },
    //前端页面配置
    logout(state){
        state.user={}
    },

    // --------------------
    //下面为管理员后台配置
    //清空vuex数据
    clearVUEX(state){
      state.catch_components = []
      state.activePath = 'adminhome'
      state.tabList = [
          {path: 'adminhome', label: '系统首页', name: 'adminhome'}
      ]
    },
    // 跳转页面执行
    selectMenu(state, submenu) {
      // 首页就是 wellcome   也就是 adminhome
      if(submenu.name === 'wellcome' || submenu.name === 'adminhome'){
          submenu.name = 'adminhome'
          submenu.path = 'adminhome'
      }
      // 当前选中菜单
      var activePath = submenu.name
      // 历史已选中菜单列表
      var oldTabList = state.tabList
      // 将菜单信息添加到tablist - 添加时判断是否已有该标签
      var result = oldTabList.some(item => {
          if(item.name === activePath){
              return true 
          } 
      })
      // 如果不包含该对象，则添加
      if(!result){
          oldTabList.push({
              path: submenu.name,
              name: submenu.name,
              label: submenu.label
          })
      }
      // 重新赋值
      state.activePath = activePath
      state.tabList = oldTabList
  },
  // 添加keepalive缓存
  addKeepAliveCache(state, val){
    // 如果是首页不缓存
    if(val === 'wellcome' || val === 'adminhome'){
        return
    }
    // 添加时判断，如果该组件已存在，则不添加
    if(state.catch_components.indexOf(val) === -1) {
        // 不存在，缓存页面
        state.catch_components.push(val)
    }
},
// 删除keepalive缓存
removeKeepAliveCache(state, val){
    let cache = state.catch_components
    for(let i = 0;i < cache.length;i++){
      if(cache[i] === val){
          cache.splice(i, 1);
      }
    }
    state.catch_components = cache
},
//关闭标签菜单
closeTab(state, val) {
    // 重新赋值
    state.activePath = val.activePath
    state.tabList = val.tabList
},
// 点击标签选择菜单
changeMenu(state, val) {
    state.activePath = val
}
  },
  actions: {
  },
  modules: {
  }
})
